<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="80" :model="queryParams">
        <el-row>
          <el-col :span="5">
            <el-form-item label="标准物质名称：" prop="bzwzName" label-width="100">
              <el-input clearable maxlength="100" placeholder="请输入标准物质名称" @keyup.enter="handleQuery"
                v-model="queryParams.bzwzName" />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="种类：" prop="bzwzType" label-width="80">
              <el-select placeholder="请选择" clearable v-model="queryParams.bzwzType" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_bzwz_type)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="唯一性编号：" prop="bzwzCode" label-width="100">
              <el-input clearable maxlength="100" placeholder="请输入唯一性编号" v-model="queryParams.bzwzCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="批次号：" prop="produceCode" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入批次号" @keyup.enter="handleQuery"
                v-model="queryParams.produceCode" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="检测项目：" prop="bucketId" label-width="80">
              <el-select v-model="queryParams.bucketId" clearable filterable remote reserve-keyword remote-show-suffix
                :remote-method="remoteMethod" :loading="loading" style="width: 100%">
                <el-option v-for="item in jcxmList" :key="item.bucketId" :value="item.bucketId" :label="item.jcxmName">
                  <span v-html="transformation(item.jcxmName)"></span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="检测项目类型：" prop="isSingle" label-width="100">
              <el-select placeholder="请选择" clearable v-model="queryParams.isSingle" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_bzwz_jcxm_type)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="归属类别：" prop="gsType" label-width="80">
              <el-select placeholder="请选择" clearable v-model="queryParams.gsType" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_bzwz_gslb)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">
            <el-form-item label="领用日期：" prop="expirationType" label-width="100">
              <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" :clearable="false"
                :editable="false" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="生产厂商：" prop="produceName">
              <el-input clearable maxlength="100" placeholder="请输入生产厂商" @keyup.enter="handleQuery"
                v-model="queryParams.produceName" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="定值单位：" prop="dzUnit">
              <el-input clearable maxlength="100" placeholder="请输入定值单位" v-model="queryParams.dzUnit"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="入库人：" prop="bzwzAdminId" label-width="80">
              <el-select v-model="queryParams.bzwzAdminId" clearable filterable placeholder="请选择">
                <el-option v-for="item in proxy.addListNoLimit(
                  userList,
                  'userId',
                  'nickName'
                )" :key="item.userId" :value="item.userId" :label="item.nickName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="领用人：" prop="createUserId">
              <el-select v-model="queryParams.createUserId" clearable filterable placeholder="请选择">
                <el-option v-for="item in proxy.addListNoLimit(
                  userList,
                  'userId',
                  'nickName'
                )" :key="item.userId" :value="item.userId" :label="item.nickName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="3">
            <el-row justify="center">
              <el-radio label="0" v-model="queryParams.isLike">模糊搜索</el-radio>
              <el-radio label="1" v-model="queryParams.isLike">精准搜索</el-radio>
            </el-row>
          </el-col>
          <el-col :span="2">
            <el-button type="success" @click="handleQuery">搜索</el-button>
            <el-button class="resetBtn" @click="resetQuery">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <p class="pinformation" style="margin-top: 8px">入库标准物质列表</p>
        <p class="totalStyle" style="margin-top: 6px">共 {{ total }} 条数据</p>
        <el-form-item label="总计入库唯一编号：" prop="" label-width="" style="margin-left: 20px">
          {{ total }}
        </el-form-item>
        <el-form-item label="入库总价（元）：" prop="" label-width="" style="margin-left: 20px">
          {{ inTotalPrice }}
        </el-form-item>
        <el-table ref="subTableRef" :data="bzwzTable">
          <el-table-column label="唯一性编号" prop="bzwzCode" align="center" />
          <el-table-column label="标准物质名称" prop="bzwzName" align="center" width="230" />
          <el-table-column label="种类" prop="bzwzType" align="center">
            <template #default="scope">
              <dict-tag :options="sys_bzwz_type" :value="scope.row.bzwzType" />
            </template>
          </el-table-column>
          <el-table-column label="生产厂商" prop="produceName" align="center" />
          <el-table-column label="批次号" prop="produceCode" align="center" />
          <el-table-column label="入库日期" prop="createDate" align="center" />
          <el-table-column label="有效日期" prop="yxDate" align="center" />
          <el-table-column label="保存位置" prop="saveLocation" align="center" />
          <el-table-column label="保存方式" prop="saveCondition" align="center" />
          <el-table-column label="规格" prop="totalGg" align="center">
            <template #default="scope">
              {{ scope.row.totalGg }}
              <span v-html="transformation(scope.row.ggUnitName)"></span>
            </template>
          </el-table-column>
          <el-table-column label="检测项目数" prop="jcxmCount" align="center">
            <template #default="scope">
              <el-button link @click="jcxmCountClick(scope.row.bzwzId)">{{
                scope.row.jcxmName ? scope.row.jcxmName : scope.row.jcxmCount
              }}</el-button>
            </template>
          </el-table-column>
          <el-table-column label="单价" prop="price" align="center" />
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>
      <el-row>
        <p class="pinformation" style="margin-top: 8px">出库标准物质列表</p>
        <p class="totalStyle" style="margin-top: 6px">
          共 {{ bottomTotal }} 条数据
        </p>
        <el-form-item label="总计领用唯一编号：" prop="" label-width="" style="margin-left: 20px">
          {{ bottomTotal }}
        </el-form-item>
        <el-form-item label="出库总价（元）：" prop="" label-width="" style="margin-left: 20px">
          {{ outTotalPrice }}
        </el-form-item>
        <el-table ref="mainTableRef" :data="bottomTable">
          <el-table-column label="唯一性编号" prop="bzwz.bzwzCode" align="center" />
          <el-table-column label="标准物质名称" prop="bzwz.bzwzName" align="center" width="230" />
          <el-table-column label="种类" prop="bzwz.bzwzType" align="center">
            <template #default="scope">
              <dict-tag :options="sys_bzwz_type" :value="scope.row.bzwz.bzwzType" />
            </template>
          </el-table-column>
          <el-table-column label="生产厂商" prop="bzwz.produceName" align="center" />
          <el-table-column label="批次号" prop="bzwz.produceCode" align="center" />
          <el-table-column label="入库日期" prop="bzwz.createDate" align="center" />
          <el-table-column label="有效日期" prop="bzwz.yxDate" align="center" />
          <el-table-column label="保存位置" prop="bzwz.saveLocation" align="center" />
          <el-table-column label="保存方式" prop="bzwz.saveCondition" align="center" />
          <el-table-column label="规格" prop="bzwz.totalGg" align="center">
            <template #default="scope">
              {{ scope.row.totalGg }}
              <span v-html="transformation(scope.row.bzwz.ggUnitName)"></span>
            </template>
          </el-table-column>
          <el-table-column label="领用量" prop="outGg" align="center">
          </el-table-column>
          <el-table-column label="检测项目数" prop="bzwz.jcxmCount" align="center">
            <template #default="scope">
              <el-button link @click="jcxmCountClick(scope.row.bzwzId)">{{
                scope.row.jcxmName ? scope.row.jcxmName : scope.row.jcxmCount
              }}</el-button>
            </template>
          </el-table-column>
          <el-table-column label="单价" prop="bzwz.price" align="center" />
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="bottomQueryParams.pageNum"
          v-model:limit="bottomQueryParams.pageSize" @pagination="getList" />
      </el-row>
    </el-card>
    <!-- 点击行内检测项目数的弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="jcxmOpen" @close="closedDialog" :title="title">
      <el-row>
        <p class="pinformation">检测项目列表</p>
        <p class="totalStyle">共 {{ multiplexTable.length }} 条数据</p>
      </el-row>
      <el-table :data="multiplexTable">
        <el-table-column label="检测项目" prop="bucketId" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.jcxmName)"></span>
          </template>
        </el-table-column>
        <el-table-column label="浓度值" prop="con" align="center" />
        <el-table-column label="浓度值单位" prop="conUnitName" align="center" />
        <el-table-column label="不确定度值" prop="notvalue" align="center" />
        <el-table-column label="不确定度单位" prop="notvalueUnit" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.subNotvalueName)"></span>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" @click="jcxmOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BzwzStatistics">
import {
  listLedger,
  bzwzList,
  listUseList,
  getTotalPrice,
  getJcxmList,
} from "@/api/resource/ledger";
import { getUnitList } from "@/api/store/unit";
import { listUser } from "@/api/contract/check";

const { proxy } = getCurrentInstance();
const { sys_bzwz_type, sys_bzwz_gslb, sys_bzwz_gqlx, sys_bzwz_jcxm_type } =
  proxy.useDict(
    "sys_bzwz_type",
    "sys_bzwz_gslb",
    "sys_bzwz_gqlx",
    "sys_bzwz_jcxm_type"
  );

const bzwzTable = ref([]);
const multiplexTable = ref([]);
const dateRange = ref([]);
const jcxmList = ref([]);
const title = ref("查看检测项目");
const jcxmOpen = ref(false);
const useOpen = ref(false);
const useTitle = ref("标准物质领用");
const data = reactive({
  queryParams: {
    pageNum: 1, // 当前页数
    pageSize: 10, // 当前页数据条数
    isLike: "0",
    bzwzType: -1,
    isSingle: -1,
    gsType: -1,
    bzwzAdminId: -1,
    createUserId: -1,
  },
});

const { queryParams } = toRefs(data);

const heightParams = ref([
  { tableName: "sub", bottomHeight: 500 },
  { tableName: "main", bottomHeight: 21 },
]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

const total = ref(0);
const bottomTotal = ref(0);
const bottomTable = ref([]);
const bottomQueryParams = ref({
  pageNum: 1,
  pageSize: 10,
  bzwz: {},
});
const inTotalPrice = ref(0);
const outTotalPrice = ref(0);

async function getList() {
  let temp = {
    "params[beginTime3]": dateRange.value[0],
    "params[endTime3]": dateRange.value[1],
  };
  Object.assign(temp, queryParams.value);
  await listLedger(temp).then((res) => {
    bzwzTable.value = res.rows;
    total.value = res.total;
  });

  bottomQueryParams.value = {
    "bzwz.bzwzName": queryParams.value.bzwzName,
    "bzwz.bzwzCode": queryParams.value.bzwzCode,
    "bzwz.bzwzType": queryParams.value.bzwzType,
    "bzwz.produceCode": queryParams.value.produceCode,
    "bzwz.jcxmName": queryParams.value.jcxmName,
    "bzwz.isSingle": queryParams.value.isSingle,
    "bzwz.gsType": queryParams.value.gsType,
    "bzwz.produceName": queryParams.value.produceName,
    "bzwz.dzUnit": queryParams.value.dzUnit,
    createUserId: queryParams.value.createUserId,
    "bzwz.bzwzAdminId": queryParams.value.bzwzAdminId,
    "bzwz.isLike": queryParams.value.isLike,
    pageNum: bottomQueryParams.value.pageNum,
    pageSize: bottomQueryParams.value.pageSize,
  };
  let temp2 = {
    "params[beginTime3]": dateRange.value[0],
    "params[endTime3]": dateRange.value[1],
  };
  Object.assign(temp2, bottomQueryParams.value);
  listUseList(
    proxy.addDateRange(bottomQueryParams.value, dateRange.value)
  ).then((res) => {
    bottomTable.value = res.rows;
    bottomTotal.value = res.total;
  });
  getTotalPrice().then((res) => {
    inTotalPrice.value = res.data.inTotalPrice;
    outTotalPrice.value = res.data.outTotalPrice;
  });
}
getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.value.isLike = "0";
  handleQuery();
}

function jcxmCountClick(row) {
  bzwzList({ bzwzId: row }).then((res) => {
    multiplexTable.value = res.data;
    jcxmOpen.value = true;
  });
}

function closedDialog() {
  multiplexTable.value = [];
}

const unitList = ref([]);
getUnitList({ unitGroup: 0 }).then((res) => {
  unitList.value = res.data;
});

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [
        { tableName: "sub", bottomHeight: 500 },
        { tableName: "main", bottomHeight: 125 },
      ];
    } else {
      heightParams.value = [
        { tableName: "sub", bottomHeight: 420 },
        { tableName: "main", bottomHeight: 21 },
      ];
    }
    getTableHeight();
  }
);

function transformation(row) {
  const a = proxy.getSupAndSub(row);
  return a;
}

const userList = ref([]);
listUser().then((res) => {
  userList.value = res.data;
});

const loading = ref(false);

function remoteMethod(val) {
  if (val) {
    loading.value = true;
    setTimeout(() => {
      getJcxmList({ jcxmName: val }).then((res) => {
        loading.value = false;
        jcxmList.value = res.rows;
      });
    }, 200);
  } else {
    jcxmList.value = [];
  }
}
</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
